<?php

/**
 * 前台积分兑换
 * File Name：RedeemController.php
 * File Encoding：UTF-8
 * File New Time：2014-6-20 14:04:43
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class RedeemController extends UserLoginBaseC {

    //兑换记录
    public function actionIndex() {
        $model = RedeemRecordsModel::model();
        $where = $model->where(array('uid' => Yii::app()->user->id));
        $count = $model->count($where);
        $pager = new CPagination($count);
        $pager->pageSize = 20;
        $pager->applyLimit($where);
        $data = $model->findAll($where);
        $this->assign('data', $data);
        $this->assign('pager', $pager);
        $this->render();
    }

    //兑换商品
    public function actionExchange() {
        $id = Yii::app()->request->getParam('id');
        if (empty($id)) {
            $this->error('请选择需要兑换的商品！');
        }
        //只允许买家会员
        if (Yii::app()->user->kind != 1) {
            $this->error('只允许买家会员参与活动！');
        }
        $model = RedeemModel::model();
        $info = $model->findByPk($id);
        if (empty($info)) {
            $this->error('该兑换商品不存在！');
        }
        if ($info->start_time > time()) {
            $this->error('还没有到兑换开始时间，开始时间：' . date('Y-m-d H:i:s', $info->start_time));
        }
        if ($info->end_time < time()) {
            $this->error('该商品兑换时间已经结束，结束时间：' . date('Y-m-d H:i:s', $info->end_time));
        }
        //检查地址是否有填写
        $userAddres = MembersAddressModel::model()->getMemberAddres(Yii::app()->user->uid);
        if (empty($userAddres->address) || empty($userAddres->consignee)) {
            $this->error('请填写收货地址等详细信息！', self::U('user/info/address'));
        }
        //旺旺，支付宝
        $memberTaobao = MembersTaobaoModel::model()->getMemberTB(Yii::app()->user->uid);
        if (empty($memberTaobao->taobao_user_nick)) {
            $this->error('请先绑定旺旺！', self::U('user/info/aliwang'));
        }
        $memberInfo = MembersModel::model()->findByPk(Yii::app()->user->uid);
        if (empty($memberInfo->alipay)) {
            $this->error('请先填写支付宝帐号信息！', self::U('user/info/alipay'));
        }
        if ($model->exchange($info) !== true) {
            $this->error($model->getOneError());
        }
        $this->success('商品兑换成功，请购买好评好，提交订单然后,耐心等待工作人员的确认及邮寄商品！', self::U($model->url));
    } 

}
